package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ShippingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(timeout = 200000)
public class ShippingServiceImpl implements ShippingService {

    @Autowired
    private ShippingDao shippingDao;

    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ExportDao exportDao;

    @Override
    public PageInfo<Shipping> findByPage(ShippingExample shippingExample, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippingList = shippingDao.selectByExample(shippingExample);
        PageInfo<Shipping> pageInfo = new PageInfo<>(shippingList);
        return pageInfo;
    }

    @Override
    public void save(Shipping shipping) {
        shipping.setState(0);
        if (shipping.getShippingMoney()==null){
            shipping.setShippingMoney(0d);
        }
        Packing packing = packingDao.selectByPrimaryKey(shipping.getShippingOrderId());
        packing.setState(1);
        packingDao.updateByPrimaryKeySelective(packing);
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(packing.getExportIds().split(",")));
        List<Export> exportList = exportDao.selectByExample(exportExample);
        if (exportList!=null && exportList.size() >0) {
            for (Export export : exportList) {
                export.setState(5);
                exportDao.updateByPrimaryKeySelective(export);
            }
        }
        shippingDao.insert(shipping);
    }

    @Override
    public Map<String, Object> delete(String shippingOrderId) {

        Map<String, Object> map = new HashMap<>();
        Shipping shipping = shippingDao.selectByPrimaryKey(shippingOrderId);
        if (shipping.getState() != 0) {
            map.put("message",0);
        } else {
            Packing packing = packingDao.selectByPrimaryKey(shippingOrderId);
            packing.setState(0);
            packingDao.updateByPrimaryKeySelective(packing);
            ExportExample exportExample = new ExportExample();
            exportExample.createCriteria().andIdIn(Arrays.asList(packing.getExportIds().split(",")));
            List<Export> exportList = exportDao.selectByExample(exportExample);
            if (exportList!=null && exportList.size()>0) {
                for (Export export : exportList) {
                     export.setState(4);
                     exportDao.updateByPrimaryKeySelective(export);
                }
            }
            shippingDao.deleteByPrimaryKey(shippingOrderId);
            map.put("message",1);
        }
        return map;
    }

    @Override
    public Shipping findById(String id) {
        return shippingDao.selectByPrimaryKey(id);
    }
}
